<?php
declare (strict_types=1);

namespace app\controller\admin;

use app\BaseController;
use think\annotation\route\Group;
use think\annotation\Route;
use \app\model\Supplier as SupplierM;

/**
 * 供应商
 * Class User
 * @package app\controller\admin
 * @Group("admin/supplier")
 */
class Supplier extends BaseController
{

    /**
     * 创建
     * @Route("index", method="get")
     */
    public function index()
    {
        //$list = config('supplier');
        //$list = (new SupplierM())->where('enable', 1)->select();
        $list = (new SupplierM())->select();

        return json_ok(['list' => $list, 'count' => count($list)], 200);
    }


    /**
     * 创建用户
     * @Route("createSupplier", method="POST")
     */
    public function create()
    {
        //接收数据
        $data = [
            'merchant_id' => input('merchantId', '', 'trim'),
            'name' => input('name', '', 'trim'),
            //'provider_id' => input('provider_id', '', 'trim'),
            'action_name' => input('action_name', '', 'trim'),
        ];

        if (empty($data['action_name']) || empty($data['merchant_id']) ||empty($data['name']) ) {
            return json_error(10002);
        }

        $supplierM = new SupplierM();
        //$oneSupplier = $supplierM->where(['name'=> $data['name'], 'merchant_id'=>$data['merchant_id'], 'enable'=>1])->find();
        $oneSupplier = $supplierM->where(['name'=> $data['name']])->find();
        if (!empty($oneSupplier)) {
            return json_error(10019);
        }

        //spplierM->where(['name'=> $data['name']])->save(['enable'=>0]);
        $result = $supplierM->save($data);
        return json_ok($result, 200);
    }

    /**
     * 修改用户信息
     * @Route("updateSupplier", method="POST")
     */
    public function update()
    {
        $id =  input('id', '', 'int');
        $name = input('name', '', 'trim');
        $merchant_id = input('merchantId', '', 'trim');
        //$provider_id = input('provider_id', '', 'trim');
        $action_name = input('action_name', '', 'trim');

        $name && $data['name'] = $name;
        $merchant_id && $data['merchant_id'] = $merchant_id;
        //$provider_id && $data['provider_id'] = $provider_id;
        $action_name && $data['action_name'] = $action_name;

        if (empty($id)) {
            return json_error(10002);
        }

        $supplierM = new SupplierM();
        //$oneSupplier = $supplierM->where(["id" => $id])->where('enable', 1)->find();
        $oneSupplier = $supplierM->where(["id" => $id])->find();
        if (empty($oneSupplier)) {
            return json_error(10001);
        }

        //$otherSupplier = $supplierM->where("id" , '<>', $id)->where(['name' => $name])->where(['merchant_id' => $merchant_id])->where('enable', 1)->find();
        $otherSupplier = $supplierM->where("id" , '<>', $id)->where(['name' => $name])->find();
        if ($otherSupplier ) {
            return json_error(10019);

        }
        $result = $oneSupplier->save($data);
        return json_ok($result, 200);
    }


}
